package edu.sctu.practice.class01.wangxiaoqiang.day20151126;

import com.google.common.base.Charsets;
import com.google.common.io.Files;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;


import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;


/**
 * Created by admin on 2015/11/26.
 */
public class GuavaClient {

    public static void main(String[] args) {
        //http://www.bxwx.org/b/70/70093/index.html
        String url = "http://www.bxwx.org/b/70/70093/index.html";
        try {
            Document document = Jsoup.connect(url).get();
            Elements elements = document.select("dd > a");
            for (Element elements1 : elements){
                String title = elements1.text();
                String deurl = elements1.attr("href");

                System.out.println(elements1.text());
                System.out.println(elements1.attr("href"));

                Files.append(elements1.text() + " " + elements1.attr("href"), new File("D:\\1212.txt"), Charsets.UTF_8);
                Files.append("\r\n",new File("D:\\1212.txt"), Charsets.UTF_8);
                getContent(title,deurl);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }



    }

    private static void getContent(String title, String deurl) {
        String ht  = "http://www.bxwx.org/b/70/70093/";
        try {
            Document document = Jsoup.connect(ht + deurl ).timeout(5000).get();
            Element elment = document.select("div#content").get(0);
            String content = elment.text();
            //Files.createParentDirs(new File("D:\\wanmeishijie\\demo.txt"));
            Files.write(content.getBytes(), new File("D:\\wenjian\\" + title + ".txt"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
